<?php
/**
 * 周舟 hzboye010@163.com
 * addby sublime snippets
 */
namespace Webview\Controller;

use Common\Api\ExchangeLogApi;
use Webview\Controller\WebViewController;
use Common\Api\ScoreGoodsApi;
use Admin\Api\MemberApi;
use Admin\Api\AddressApi;
use Tool\Api\AreaApi;
use Tool\Api\CityApi;
use Tool\Api\ProvinceApi;

class ExchangeLogController extends WebviewController{
    /**
     * 初始化
     */
    public function _initialize(){
        parent::_initialize();
        // $this->assign('uid',$this->uid);
        // $this->assign('psw',$this->psw);
        // $this->assign('isapp','com.itboye.sunsun://?type=');
        // $this->assign('isapp','');
    }
    /**
     * 兑换记录页面入口
     */
    public function index(){
        // $uid = $this->uid;
        // echo '2323';exit;
        $this->assignTitle('兑换记录');
        $this->display('ExchangeLog/index');
    }

    public function payBack($info){
      // '支付成功，请耐心等待工作人员发货'
      // $this->redirect());
      $this->assignTitle($info);
      $this->assign('info',$info);
      // $this->assign('url',U('Webview/ExchangeLog/index',array('uid'=>$this->uid,'psw'=>$this->psw)));
      $this->display('payBack');
    }

    //删除 已发货的兑换记录 （只是将exchange_status 改为-2）
    public function delete($id){
      $r = apiCall(ExchangeLogApi::DELETE,array($id));
      if($r['status']) $this->redirect('ExchangeLog/index',array('uid'=>$this->uid,'psw'=>$this->psw));
      else $this->error('删除失败');
    }

    public function pay($id){
     $this->assignTitle('积分兑换支付页面');
     $this->assign('id',$id);
     $uid = $this->uid;
     $r = apiCall(ExchangeLogApi::GET_INFO,array(array('id'=>$id,'uid'=>$uid,'exchange_status'=>-1)));
     if(!$r['status']) $this->error('系统错误：ExchangeLog:find:error');
     elseif(!$r['info']) $this->error('兑换记录状态错误！');
     elseif(!$r['info']['price']) $this->error('此单无需支付');
     elseif($r['info']['exchange_status']>-1 && $r['info']['pay_status']) $this->error('此单已经支付');
     // dump($r['info']);exit;
     $pid = $r['info']['pid'];
     $member = apiCall(MemberApi::GET_INFO,array(array('uid'=>$uid)));
     $sgoods = apiCall(ScoreGoodsApi::GET_INFO,array(array('pid'=>$pid)));

     $url = apiCall(ScoreGoodsApi::GETPAYURL,array($uid,$sgoods['info']['price'],$this->psw));
     // if($url[''])
     $r2 = apiCall(ExchangeLogApi::SAVEBYID,array($id,array('pay_code'=>$url['code'])));
     if(!$r2['status']) $this->error('订单写入失败');
     unset($r2);
     $this->assign('payurl',$url['url']);
     $this->assign('aid',$r['info']['address_id']);
     $this->assign('user',$member['info']);
     $this->assign('goods',$sgoods['info']);
     $this->assign('exchange',$r['info']);
      //查询发货地址
      $r = apiCall(AddressApi::QUERY_NO_PAGING,array(array('uid'=>$uid)));
      if(!$r['status']) $this->error('系统错误: ADDRESS GET ERROR');
      // dump($r['info']);
      $this->assign('address',$r['info']);
      //查询省份
      $r = apiCall(ProvinceApi::QUERY_NO_PAGING,array());
      if(!$r['status']) $this->error('系统错误: Province GET ERROR');
      $this->assign('topCats',$r['info']);
      $this->display('pay');
    }
    /**
     * ajax获取兑换记录
     * @param  int      the start position
     * @param  int      pagesize
     * @return [json]
    */
    public function getLogs($start,$size){
        //  dump($this->uid);exit;
        echo json_encode(apiCall(ExchangeLogApi::QUERY,array($this->uid,$start,$size)));
    }

    public function detail($id){
      $this->assignTitle('兑换详情');
      $r = apiCall(ExchangeLogApi::GET_INFO,array(array('id'=>$id)));
      // echo json_encode();
      if($r['status']) $this->assign('entry',$r['info']);
      $this->display('ExchangeLog/detail');
    }

    // public function test($uid,$pid){
    public function test(){
      $this->redirect('Webview/ExchangeLog/payBack',array('info'=>'支付成功'));
    //echo json_encode(apiCall(GameApi::TEST,array($uid,$gid)));
    //echo '';
    }
}